package com.mvity.framework.common.base;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hibernate.annotations.GenericGenerator;

import com.mvity.framework.common.global.Constants;
import com.mvity.framework.system.entity.SystemUser;
import com.mvity.framework.system.vo.UserVO;

/**
 * 实体 基类
 * 
 * @author vity
 * @date 2012-3-15
 * @mail vity.1215@gmail.com
 */
@MappedSuperclass
public abstract class BaseEntity implements Serializable, Comparable<BaseEntity> {

	protected static final long serialVersionUID = -7910675456640125180L;

	public BaseEntity() {
		this.isDelete = Constants.NO;
		this.createTime = new Date();
		this.lastModifyTime = this.createTime;
		this.orderIndex = 0;
		this.dataState = Constants.DEFAULT_DATA_STATE;
	}

	public void setOperator(SystemUser user) {
		this.setOperator(UserVO.fromEntity(UserVO.class, user));
	}

	public void setOperator(UserVO user) {
		if (StringUtils.isBlank(this.getId())) {
			this.createUserId = user.getId();
			this.tenantId = user.getTenantId();
		}
		this.modifyUserId = user.getId();
		this.lastModifyTime = new Date();
	}

	/**
	 * 数据表主键ID
	 */
	@Column(name = "id", length = 32, nullable = false, updatable = false)
	@Id
	@GenericGenerator(name = "idGenerator", strategy = "uuid")
	@GeneratedValue(generator = "idGenerator")
	private String id;

	/**
	 * 数据版本
	 */
	@Column(name = "version", nullable = false)
	@Version
	private int version;

	/**
	 * 数据创建时间
	 */
	@Column(name = "create_time", nullable = false, updatable = false)
	@Temporal(TemporalType.TIMESTAMP)
	private Date createTime;

	/**
	 * 数据最近更新时间
	 */
	@Column(name = "last_modify_time", nullable = false)
	@Temporal(TemporalType.TIMESTAMP)
	private Date lastModifyTime;

	/**
	 * 数据是否删除状态
	 */
	@Column(name = "is_delete", nullable = false)
	private String isDelete;

	/**
	 * 预留数据列
	 */
	@Column(name = "reserve_column", length = 300)
	private String reserveColumn;

	/**
	 * 权重索引列
	 */
	@Column(name = "order_index")
	private Integer orderIndex;

	/**
	 * 数据项状态
	 */
	@Column(name = "data_state", length = 100)
	private String dataState;

	/**
	 * 所属租户
	 */
	@Column(name = "tenant_id", length = 32)
	private String tenantId;

	/**
	 * 创建用户
	 */
	@Column(name = "create_user_id", length = 32)
	private String createUserId;

	/**
	 * 修改用户
	 */
	@Column(name = "modify_user_id", length = 32)
	private String modifyUserId;

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the createTime
	 */
	public Date getCreateTime() {
		return createTime;
	}

	/**
	 * @param createTime
	 *            the createTime to set
	 */
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	/**
	 * 返回 lastModifyTime 的值
	 * 
	 * @return lastModifyTime
	 */
	public Date getLastModifyTime() {
		return lastModifyTime;
	}

	/**
	 * 设置 lastModifyTime 的值
	 * 
	 * @param lastModifyTime
	 */
	public void setLastModifyTime(Date lastModifyTime) {
		this.lastModifyTime = lastModifyTime;
	}

	/**
	 * @return the version
	 */
	public int getVersion() {
		return version;
	}

	/**
	 * @param version
	 *            the version to set
	 */
	public void setVersion(int version) {
		this.version = version;
	}

	/**
	 * @return the isDelete
	 */
	public boolean isDelete() {
		return Boolean.valueOf(this.isDelete);
	}

	/**
	 * @param isDelete
	 *            the isDelete to set
	 */
	public void setDelete(Boolean isDelete) {
		this.isDelete = String.valueOf(isDelete);
	}

	/**
	 * @return the reserveColumn
	 */
	public String getReserveColumn() {
		return reserveColumn;
	}

	/**
	 * @param reserveColumn
	 *            the reserveColumn to set
	 */
	public void setReserveColumn(String reserveColumn) {
		this.reserveColumn = reserveColumn;
	}

	/**
	 * 返回 orderIndex 的值
	 * 
	 * @return orderIndex
	 */
	public Integer getOrderIndex() {
		return orderIndex;
	}

	/**
	 * 设置 orderIndex 的值
	 * 
	 * @param orderIndex
	 */
	public void setOrderIndex(Integer orderIndex) {
		this.orderIndex = orderIndex;
	}

	/**
	 * 返回 dataState 的值
	 * 
	 * @return dataState
	 */
	public String getDataState() {
		return dataState;
	}

	/**
	 * 设置 dataState 的值
	 * 
	 * @param dataState
	 */
	public void setDataState(String dataState) {
		this.dataState = dataState;
	}

	/**
	 * @return the tenantId
	 */
	public String getTenantId() {
		return tenantId;
	}

	/**
	 * @param tenantId
	 *            the tenantId to set
	 */
	public void setTenantId(String tenantId) {
		this.tenantId = tenantId;
	}

	/**
	 * @return the createUserId
	 */
	public String getCreateUserId() {
		return createUserId;
	}

	/**
	 * @param createUserId
	 *            the createUserId to set
	 */
	public void setCreateUserId(String createUserId) {
		this.createUserId = createUserId;
	}

	/**
	 * @return the modifyUserId
	 */
	public String getModifyUserId() {
		return modifyUserId;
	}

	/**
	 * @param modifyUserId
	 *            the modifyUserId to set
	 */
	public void setModifyUserId(String modifyUserId) {
		this.modifyUserId = modifyUserId;
	}

	/*
	 * 依据 orderIndex 属性进行降序排列
	 * 
	 * @param o
	 * 
	 * @return
	 * 
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	@Override
	public int compareTo(BaseEntity o) {
		return o.getOrderIndex().compareTo(this.getOrderIndex());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + version;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		BaseEntity other = (BaseEntity) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (version != other.version)
			return false;
		return true;
	}

}
